import request from '@/router/axios';

export const getList = (current, size, params) => {
  return request({
    url: '/api/vingsoft-acceptance/materialFile/list',
    method: 'get',
    params: {
      ...params,
      current,
      size,
    }
  })
}

export const getDetail = (id) => {
  return request({
    url: '/api/vingsoft-acceptance/materialFile/detail',
    method: 'get',
    params: {
      id
    }
  })
}

export const remove = (ids) => {
  return request({
    url: '/api/vingsoft-acceptance/materialFile/remove',
    method: 'post',
    params: {
      ids,
    }
  })
}

export const add = (row) => {
  return request({
    url: '/api/vingsoft-acceptance/materialFile/submit',
    method: 'post',
    data: row
  })
}

export const update = (row) => {
  return request({
    url: '/api/vingsoft-acceptance/materialFile/submit',
    method: 'post',
    data: row
  })
}

/*后台调用*/
export const onlinePreview = (linkUrl,fullfilename) => {
  //fullfilename=test.png
  return request({
    url: '/api/vingsoft-ywsp/kkfileView/getFileViewInfo',
    method: 'get',
  }).then(res => {
    if(res.data.data.onlinePreviewVersions === '2.x'){
      if(matchType(linkUrl)){
        window.open(res.data.data.onlinePreviewUrl+'?url='+encodeURIComponent(linkUrl));
      }else{
        window.open(res.data.data.onlinePreviewUrl+'?url='+encodeURIComponent(linkUrl+"&fullfilename="+fullfilename));

      }
    }else{
      if(matchType(linkUrl)){
        window.open(res.data.data.onlinePreviewUrl+'?url='+encodeURIComponent(Base64.encode(linkUrl)));
      }else{
        window.open(res.data.data.onlinePreviewUrl+'?url='+encodeURIComponent(Base64.encode(linkUrl+"&fullfilename="+fullfilename)));
      }
    }
  })

}



//判断附件url通过附件code获取附件还是url附件路径
export function matchType  (linkUrl){
  //获取后缀
  let suffix = linkUrl.split(".");

  let result = false ;

  //预览服务支持的附件名
  let fileList = ['txt','java','php','py','md','js','css','pdf','doc','docx','xls','xlsx','zip','rar','jar','tar','gzip',
    'jpg','jpeg','png','gif','mp3','mp4','flv','ofd'];

  result  = fileList.some(function (item){
    return item == suffix[suffix.length -1];
  })

  return result;

}


//上传文件之前的钩子，参数为上传的文件，可用作文件大小类型的判断，若返回 false 或者返回 Promise 且被 reject，则停止上传。	function(file)
export function caseMaterialbeforeAvatarUpload(file,_this) {
  console.log("文件", file);
  // 判断图片格式
  const isType =
    file.type === "image/png" ||
    file.type === "image/jpg" ||
    file.type === "image/jpeg"||
    file.type === "image/bmp"||
    file.type === "text/plain"||//.txt
    file.type === "application/vnd.ms-powerpoint"||//.ppt
    file.type === "application/msword"||//.doc
    file.type === "application/vnd.ms-excel"||//.xls
    file.type === "application/vnd.openxmlformats-officedocument.presentationml.presentation"||//.PPTX
    file.type === "application/vnd.openxmlformats-officedocument.wordprocessingml.document"||//.docx
    file.type === "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"||//.xlsx
    file.type === "application/zip"|| file.type === "application/x-zip-compressed" ||
    file.type === "application/pdf";

  // 判断上传图片大小
  const isLt50M = file.size / 1024 / 1024 <= 50;
  if (!isType) {
    _this.$message.error("上传文件格式不符合要求");
    return false;
  }
  if (!isLt50M) {
    _this.$message.error("上传文件大小不能超过 50MB");
    return false;
  }
  return isType && isLt50M;
}

